package cn.jiyun.service.impl;

import cn.jiyun.dao.MemberDao;
import cn.jiyun.dao.OrderDao;
import cn.jiyun.dao.SetmealDao;
import cn.jiyun.pojo.HotSetmeal;
import cn.jiyun.pojo.ReportData;
import cn.jiyun.pojo.Setmeal;
import cn.jiyun.serivce.ReportService;
import cn.jiyun.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private SetmealDao setmealDao;

    @Override
    public Integer getCountByRegDate(String regDate) {
        return memberDao.getCountByRegDate(regDate);
    }

    @Override
    public List<Map<String, Object>> getSetmealReport() {
        return memberDao.getSetmealReport();
    }

    @Override
    public ReportData getBusinessReportData() throws Exception {
        ReportData reportData = new ReportData();
        Date today = DateUtils.getToday();
        Date firstDayOfWeek = DateUtils.getFirstDayOfWeek(today);
        Date firstDay4ThisMonth = DateUtils.getFirstDay4ThisMonth();
        Date lastDayOfWeek = DateUtils.getLastDayOfWeek(today);
        Date lastDayOfMonth = DateUtils.getLastDayOfMonth(today);

        String todayStr = DateUtils.parseDate2String(today);
        reportData.setReportDate(todayStr);

//        会员数据统计
//        新增会员数 当日新增
        Integer todayNewMember = memberDao.getTodayNewMember(todayStr);
        reportData.setTodayNewMember(todayNewMember);

        //总会员数
        Integer totalMember = memberDao.getTotalMember();
        reportData.setTotalMember(totalMember);

        //本周新增会员数 查询注册时间大于等于本周第一天
        Integer thisWeekNewMember = memberDao.getThisWeekNewMember(firstDayOfWeek);
        reportData.setThisWeekNewMember(thisWeekNewMember);

        //本月新增会员数 查询注册时间大于等于本周第一天
        Integer thisMonthNewMember = memberDao.getThisWeekNewMember(firstDay4ThisMonth);
        reportData.setThisMonthNewMember(thisMonthNewMember);

//        预约到诊数据统计
//        今日预约数
        Integer todayOrderNumber = orderDao.getTodayOrderNumber(todayStr);
        reportData.setTodayOrderNumber(todayOrderNumber);

//        今日到诊数
        Integer todayVisitsNumber = orderDao.getTodayVisitsNumber(todayStr);
        reportData.setTodayVisitsNumber(todayVisitsNumber);

//        本周预约数
        Integer thisWeekOrderNumber = orderDao.getThisWeekOrderNumber(firstDayOfWeek,lastDayOfWeek);
        reportData.setThisWeekOrderNumber(thisWeekOrderNumber);

//        本周到诊数
        Integer thisWeekVisitsNumber = orderDao.getThisWeekVisitsNumber(firstDayOfWeek,lastDayOfWeek);
        reportData.setThisWeekVisitsNumber(thisWeekVisitsNumber);

//        本月预约数
        Integer thisMonthOrderNumber = orderDao.getThisWeekOrderNumber(firstDay4ThisMonth,lastDayOfMonth);
        reportData.setThisMonthOrderNumber(thisMonthOrderNumber);

        //        本月到诊数
        Integer thisMonthVisitsNumber = orderDao.getThisWeekVisitsNumber(firstDay4ThisMonth,lastDayOfMonth);
        reportData.setThisMonthVisitsNumber(thisMonthVisitsNumber);

//        热门套餐
        List<HotSetmeal> hotSetmealList = setmealDao.getHotSetmealList();
        reportData.setHotSetmeal(hotSetmealList);

        return reportData;
    }
}
